# -*- coding: utf-8 -*-

"""
@Author      : Sky Bully
@Time        : 2024/6/12 20:40
"""
import os
import sys
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

# 字体加载
from matplotlib import font_manager, rcParams

font_path = r"./font.ttf"
font_manager.fontManager.addfont(font_path)
prop = font_manager.FontProperties(fname=font_path)

# 字体设置
rcParams['font.family'] = 'sans-serif'  # 使用字体中的无衬线体
rcParams['font.sans-serif'] = prop.get_name()  # 根据名称设置字体
rcParams['font.size'] = 10  # 设置字体大小
rcParams['axes.unicode_minus'] = False  # 使坐标轴刻度标签正常显示正负号


def each_reac_plot(reac_file, start_date, end_date, out_loc):
    df = pd.read_csv(reac_file)
    df = df[df['caseName'] == 'base']

    ticks = pd.date_range(start=start_date, end=end_date, freq='6h')
    ticks = [t.strftime('%#m/%#d-%#H时') for t in ticks]
    len_tick = len(ticks)

    x = range(len(df['t']))
    ho2no = df['HO2NO'].values
    ro2no = df['RO2NO'].values
    vocsno3 = df['VOCsNO3'].values * (-1)
    vocso3 = df['VOCsO3'].values * (-1)
    o1dh2o = df['O1DH2O'].values * (-1)
    oho3 = df['O3OH'].values * (-1)
    ho2o3 = df['HO2O3'].values * (-1)
    ohno2 = df['NO2OH'].values * (-1)

    # 绘制堆积面积图
    # fig = plt.figure(figsize=(9, 2))
    fig = plt.figure()
    ax1 = fig.add_subplot(111)
    ax1.stackplot(x, [ho2no, ro2no], labels=['HO$_{2}$+NO', 'RO$_{2}$+NO'], colors=['#61de2a', '#a8ff04'])  # color=['ff000d', 'purple']
    ax1.stackplot(x, [vocsno3, vocso3, o1dh2o, oho3, ho2o3, ohno2], colors=['#13eac9', '#ff796c', '#ff000d', '#0c06f7', '#fac205', '#a2a415'],
                  labels=['VOCs+NO$_{3}$', 'VOCs+O$_{3}$', 'O1D+H$_{2}$O', 'O$_{3}$+OH', 'HO$_{2}$+O$_{3}$', 'NO$_{2}$+OH'])  #color=['#13eac9', '#ff796c', '#0165fc', '#c04e01', '#fac205', '#a2a415'],

    ax1.plot(x, ho2no + ro2no+vocsno3+vocso3+o1dh2o+oho3+ho2o3+ohno2, color='black', marker='o', markerfacecolor='red', markersize=4, label='Net(O$_{3}$)')
    plt.margins(x=-0.01)
    ax1.set_ylim(-10, 40)
    plt.ylabel('速率(ppb/h)', fontsize=14)  # 纵坐标轴标题
    plt.xticks(np.arange(0, len(df['t']), 6), ticks[:(len_tick-1)], rotation=0, fontsize=14)
    plt.legend(loc='upper center', ncol=5, fontsize=8)
    plt.savefig(os.path.join(out_loc, 'each_reac.png'), bbox_inches='tight', dpi=600)


if __name__ == '__main__':
    if len(sys.argv) < 5:
        print(f"[Error]: Run with python {sys.argv[0]} './5_each_reac.csv' '2023-05-16' '2023-05-17' './picture'")
    else:
        each_reac_plot(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4])
